<?php

class VideoaulasController extends Site_Controller_Action
{
    public $limitRegistros = 4;

    public function init()
    {
        
    }

    public function indexAction()
    {
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/videoaulas.js');
        
        $obj = new Ead_Model_DbTable_Catcurso();
        $this->view->filtro = $obj->fetchAll()->toArray();
        
        $obj = new Ead_Model_DbTable_Videoaulas();
        $result = $obj->fetchAll($obj->select()->limit($this->limitRegistros,0)->order('VID_ORD ASC'));
        
        $this->view->result = $result; 
                
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $btn_mais = 0;
        }else if($count < 4) {
            $btn_mais = 0;
        }
        
        $this->view->btn_mais = $btn_mais;
        
        $this->view->submenu_page = TRUE;
    }
    
     public function filtroAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        
        $filtro = $this->_getParam('filtro', 0);
        
        $where = 'CTC_ID > 0';
        $filtro = (int)$this->_getParam('filtro', 0);
        if($filtro > 0) $where = 'CTC_ID like "%'.$filtro.'%"';
        
        $obj = new Ead_Model_DbTable_Videoaulas();
        $result = $obj->fetchAll($obj->select()->where($where)->limit($this->limitRegistros,0));
        
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $offset = 0;
            $btn_mais = 0;
        } else if($count < 4) {
            $btn_mais = 0;
        }
        
        $html =  $this->view->partial('videoaulas/list-vid.phtml', array('urlbase' => $this->_urlbase,'controller' => $this->_controller,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'btn_mais'=>$btn_mais));
    }
     
    public function detalhesAction(){
        
        $id = $this->_getParam('id');
        
        $this->verificaLogin(NULL,$id);
        
        if ($id > 0) {
           
            Zend_Loader::loadClass('estatisticas','library');
            $obj = new Estatisticas();
            $obj->acesso_pagina($id,2); 
            
            $obj = new Ead_Model_DbTable_Videoaulas();
            $row = $obj->fetchRow('VID_ID = ' . $id);

            $this->view->row = $row;
            
            if($row->VID_CENE == 1 && Zend_Auth::getInstance()->getStorage()->read()->USU_CENE == 0){
                $this->_helper->redirector('index', $this->_controller,array());
            }
            
            $rep = $row->findDependentRowSet("Ead_Model_DbTable_Repositorio"); 

            $sessao = new Zend_Session_Namespace('Repositorio');
            $sessao->caminho = '..'.$this->_urlbase.'/public/upload/repositorio/';
            $sessao->id = $rep[0]['REP_ID'];
            $sessao->dathor = $rep[0]['REP_DATHOR'];
            //echo $sessao->caminho.md5($sessao->id.$sessao->dathor);
 
        }
    }
    
    public function maismateriaisAction(){
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        
        $offset = (int)$this->_getParam('offset', 0) + $this->limitRegistros;
        
        $where = 'CTC_ID > 0';
        $filtro = (int)$this->_getParam('filtro', 0);
        if($filtro > 0) $where = 'CTC_ID like "%'.$filtro.'%"';
        
        $obj = new Ead_Model_DbTable_Videoaulas();
        $result = $obj->fetchAll($obj->select()->where($where)->limit($this->limitRegistros,$offset));
        
        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res) $count += 1;
        if($count == 0){
            $offset = $offset - $this->limitRegistros;
            $btn_mais = 0;
        }
        else if($count < 4) {
            $btn_mais = 0;
        }
              
        $html =  $this->view->partial('videoaulas/list-vid.phtml', array('urlbase' => $this->_urlbase,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'offset'=>$offset,'btn_mais'=>$btn_mais));
        
    }

}

